package com.gaga.console.base.config;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaga.console.core.entity.SysUserRole;
import com.gaga.console.core.service.SysRoleMenuService;
import com.gaga.console.core.service.SysUserRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.dev33.satoken.stp.StpInterface;

/**
 * 自定义权限验证接口扩展 
 */
@Component    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysUserRoleService sysUserRoleService;
    /**
     * 返回一个账号所拥有的权限码集合 
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> list = new ArrayList<String>();
        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 感觉每次都要查询怪怪的，考虑缓存机制
        List<String> list = new ArrayList<String>();
        List<SysUserRole> roleList = sysUserRoleService.list(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, Long.valueOf((String) loginId)));

        if(roleList != null && roleList.size() > 0){
            List<String> roleIdList = roleList.stream().map(role -> role.getRoleId().toString()).collect(Collectors.toList());
            list.addAll(roleIdList);
        }
        return list;
    }

}
